home *** CD-ROM | disk | FTP | other *** search
/ MacHack 1994 / MacHack 1994.toast / MacHack™94 / Talks & Papers / Michael D. Crawford↵ / Word Services SDK 1.0.5 / Writeswell Jr. Source / PrintWWJr.c < prev    next >
Text File  |  1993-03-17  |  7KB  |  340 lines

  1. /* PrintWWJr.c
  2.  * Handle printing in Writeswell Jr.
  3.  *
  4.  * ©1992 Working Software, Inc.
  5.  * This source code is copyrighted.  Permission is granted to use the Word Services
  6.  * portion of the Writeswell Jr. source code in your own programs, but you 
  7.  * may not distribute the Writeswell Jr. word-processor code as a 
  8.  * commercial product.  If you modify the code, please do not call it 
  9.  * Writeswell Jr. (or Writeswell.)  This will ensure that people understand the 
  10.  * program and don’t have to deal with a number of different versions with 
  11.  * who-knows-what going on in the code.
  12.  * 
  13.  * Writeswell Jr. and Writeswell are trademarks of Working Software, Inc.
  14.  *
  15.  * 17 Oct 92 Mike Crawford - The third anniversary of the Loma Prieta Earthquake.
  16.  *                            Downtown Santa Cruz is still under reconstruction.
  17.  */
  18.  
  19. #include <EPPC.h>
  20. #include <AppleEvents.h>
  21. #include <AEObjects.h>
  22. #include <Printing.h>
  23. #include "AERegistry.h"
  24. #include "WordServices.h"
  25. #include "TestBed.h"
  26. #include "TBConstants.h"
  27. #include "Gripe.h"
  28. #include "Scroll.h"
  29. #include "Prefs.h"
  30. #include "TBGlobals.h"
  31. #include "PrintWWJr.h"
  32. #include "UnloadStuff.h"
  33. #include "PageSetup.h"
  34.  
  35. void PostPrintingErrors( OSErr theErr );
  36. void PrintPage( Rect rPage, short pageNumber, Boolean *morePages );
  37. void MakePrinterText( THPrint thePrRecHdl );
  38. void SetPrintRect( Rect *rPagePtr );
  39. void SetWindowRect( void );
  40.  
  41. static Rect    gOldDestRect;
  42. static Rect gOldViewRect;
  43.  
  44. void DoPrint( void )
  45. {
  46.     GrafPtr        oldPort;
  47.     short        copies;
  48.     short        firstPage;
  49.     short        lastPage;
  50.     short        numberOfCopies;
  51.     short        printmgrsResFile;
  52.     short        realNumberOfPagesInDoc;
  53.     short        pageNumber;
  54.     short        printError;
  55.     THPrint        thePrRecHdl;
  56.     TPPrPort    thePrPort;
  57.     TPrStatus    theStatus;
  58.     Rect        rPage;
  59.     Boolean        morePages;
  60.  
  61.     GetPort( &oldPort );
  62.  
  63.     thePrRecHdl = GetPrintRecord();
  64.  
  65.     if ( !thePrRecHdl ){
  66.         Gripe( "\pCannot get print record" );
  67.         return;
  68.     }
  69.  
  70.     UnloadForPrint();
  71.         
  72.     PrOpen();
  73.     
  74.     if ( PrError() == noErr ){
  75.     
  76.         printmgrsResFile = CurResFile();
  77.         
  78.         if ( PrJobDialog( thePrRecHdl ) ){
  79.         
  80.             numberOfCopies = (**thePrRecHdl).prJob.iCopies;
  81.             firstPage = (**thePrRecHdl).prJob.iFstPage;
  82.             lastPage = (**thePrRecHdl).prJob.iLstPage;
  83.                     
  84.             /* STUB show printing status dialog */
  85.             
  86.             for ( copies = 1; copies <= numberOfCopies; copies++ ){
  87.             
  88.                 /* STUB install idle proc */
  89.                 
  90.                 /* Restore the resource file to the printer driver */
  91.                 
  92.                 UseResFile( printmgrsResFile );
  93.                 
  94.                 thePrPort = PrOpenDoc( thePrRecHdl,
  95.                                         (TPPrPort)NULL,
  96.                                         (Ptr)NULL );
  97.                 
  98.                 pageNumber = firstPage;
  99.                 
  100.                 rPage = (**thePrRecHdl).prInfo.rPage;
  101.                 
  102.                 SetPrintRect( &rPage );        /* Set TE Rec to fit on printer paper */
  103.  
  104.                 morePages = true;
  105.  
  106.                 while ( pageNumber <= lastPage && morePages && PrError() == noErr ){
  107.                     PicHandle    thePict;
  108.                     GrafPtr        curPort;
  109.                     
  110.                     GetPort( &curPort );
  111.                     SetPort( gDocWindow );
  112.                 
  113.                     thePict = OpenPicture( &rPage );
  114.                     
  115.                     /*ClipRect( &(thePort->portRect) );*/
  116.                     /*ClipRect( &rPage );*/
  117.                     
  118.                     if ( !thePict ){
  119.                         PrSetError( memFullErr );
  120.                     }else{
  121.                         PrintPage( rPage, pageNumber, &morePages );
  122.                         ClosePicture();
  123.                         
  124.                         SetPort( curPort );
  125.  
  126.                         PrOpenPage( thePrPort, (TPRect)NULL );
  127.                         
  128.                         if ( PrError() == noErr ){
  129.                             DrawPicture( thePict, &rPage );
  130.                         }
  131.                         
  132.                         PrClosePage( thePrPort );
  133.                         
  134.                         DisposeHandle( thePict );
  135.                     }
  136.                     pageNumber++;
  137.                 }/* while pageNumber */
  138.                 
  139.                 SetWindowRect();            /* Restore TE Rec to fit in window */
  140.  
  141.                 PrCloseDoc( thePrPort );
  142.  
  143.                 if (!PrError() &&
  144.                         ((*thePrRecHdl)->prJob.bJDocLoop == bSpoolLoop)){
  145.  
  146.                     PrPicFile( thePrRecHdl, 
  147.                                 (TPrPort*)NULL, 
  148.                                 (Ptr)NULL, 
  149.                                 (Ptr)NULL, 
  150.                                 &theStatus);
  151.                 }
  152.  
  153.  
  154.             }/* for copies */
  155.         }else{
  156.             /* The print job is being canceled by the user
  157.              * from the job dialog
  158.              */
  159.             PrSetError( iPrAbort );
  160.         }
  161.     }/* If PrError */
  162.     
  163.     printError = PrError();
  164.     
  165.     PrClose();
  166.     
  167.     /* 1.0.2 Check for iPrAbort - user canceled Job dialog or pressed Cmd Dot */
  168.  
  169.     if ( printError != noErr && printError != iPrAbort )
  170.         PostPrintingErrors( printError );
  171.     
  172.     ReleaseResource( thePrRecHdl );
  173.     
  174.     /* STUB dispose of progress dialog */
  175.  
  176.     SetPort( oldPort );
  177.  
  178.     return;
  179. }
  180.  
  181. void PostPrintingErrors( OSErr theErr )
  182. {
  183.     /* STUB Explain what the error really was */
  184.     
  185.     Gripe( "\pAn error occurred while printing" );
  186.  
  187.     return;
  188. }
  189.  
  190. void SetPrintRect( Rect *rPagePtr )
  191. {
  192.     TEHandle    textH;
  193.  
  194.     /* Resize the TextEdit record's dest and view rects to fit the printer paper */
  195.  
  196.     textH = (TEHandle)GetWRefCon( gDocWindow );
  197.     
  198.     gOldDestRect = (*textH)->destRect;
  199.     gOldViewRect = (*textH)->viewRect;
  200.     
  201.     (*textH)->destRect = *rPagePtr;
  202.     (*textH)->viewRect = *rPagePtr;
  203.  
  204.     TECalText( textH );
  205.  
  206.     return;
  207. }
  208.  
  209. void SetWindowRect( void )
  210. {
  211.     TEHandle    textH;
  212.     GrafPtr        curPort;
  213.     
  214.     /* Restore the TE rec's original view and dest rects */
  215.  
  216.     GetPort( &curPort );
  217.     SetPort( gDocWindow );
  218.  
  219.     textH = (TEHandle)GetWRefCon( gDocWindow );
  220.     
  221.     (*textH)->destRect = gOldDestRect;
  222.     (*textH)->viewRect = gOldViewRect;
  223.  
  224.     TECalText( textH );
  225.  
  226.     /* Force a redraw of the text */
  227.     InvalRect( &gOldViewRect );
  228.     
  229.     SetPort( curPort );
  230.  
  231.     return;
  232. }
  233.  
  234. void PrintPage( Rect rPage, short pageNumber, Boolean *morePages )
  235. {
  236.     TEHandle    textH;
  237.     GrafPtr        curPort;
  238.     Rect        clipRect;
  239.     short        docHeight;
  240.     short        pageHeight;
  241.     Rect        viewRect;
  242.     Rect        destRect;
  243.  
  244.     textH = (TEHandle)GetWRefCon( gDocWindow );
  245.  
  246.     docHeight = TEGetHeight( 1, (*textH)->nLines, textH );
  247.     
  248.     pageHeight = rPage.bottom - rPage.top;
  249.     
  250.     if ( pageHeight * pageNumber > docHeight ){
  251.         *morePages = false;
  252.     }else{
  253.         *morePages = true;
  254.     }
  255.  
  256.     SetRect( &clipRect, 0, 0, 0, 0 );
  257.     ClipRect( &clipRect );
  258.     
  259.     viewRect = (*textH)->viewRect;
  260.     destRect = (*textH)->destRect;
  261.  
  262.     TEScroll( 0, viewRect.top - destRect.top, textH );
  263.     TEScroll( 0, - ( pageHeight * (pageNumber - 1) ), textH );
  264.  
  265.     clipRect = rPage;
  266.  
  267. /*
  268.     clipRect.top += (pageHeight * (pageNumber - 1) );    
  269.     clipRect.bottom += (pageHeight * (pageNumber - 1) );
  270. */
  271.  
  272.     ClipRect( &clipRect );
  273.  
  274.     TEUpdate( &rPage, textH );
  275.             
  276.     return;
  277. }
  278.  
  279. #ifdef NEVER
  280. void MakePrinterText( THPrint thePrRecHdl )
  281. {
  282.     Rect            destRect;
  283.     Rect            viewRect;
  284.     CharsHandle        textHdl;
  285.     TEStyleHandle    stylHdl;
  286.     TEHandle        winTEHdl;
  287.     long            textLen;
  288.     short            oldStart;
  289.     short            oldEnd;
  290.  
  291.     /* If there are any errors, we post them into the printer manager, so
  292.      * that upon return, the error is checked and we will exit the printing loop.
  293.      * I think that should work properly.
  294.      *
  295.      * It will have the advantage that the printing loop will be entirely exited
  296.      * before the error is actually displayed.
  297.      */
  298.  
  299.     destRect = (*thePrRecHdl)->prInfo.rPage;
  300.     viewRect = destRect;
  301.  
  302.     gPrintTextHdl = TEStylNew( &destRect, &viewRect );
  303.     
  304.     if ( !gPrintTextHdl ){
  305.         PrSetError( memFullErr );
  306.         return;
  307.     }
  308.  
  309. #ifdef NEVER    
  310.     /* Copy the text from the window's TE rec to the printer's TE rec */
  311.     
  312.     winTEHdl = (TEHandle)GetWRefCon( gDocWindow );
  313.  
  314.     textHdl = TEGetText( winTEHdl );
  315.     
  316.     textLen = GetHandleSize( textHdl );
  317.     
  318.     HLock( textHdl );
  319.     TEInsert( *textHdl, textLen, gPrintTextHdl );
  320.     HUnlock( textHdl );
  321.     
  322.     /* Copy the styles from the window's TE rec to the printer's */
  323.  
  324.     oldStart = (*winTEHdl)->selStart;
  325.     oldEnd = (*winTEHdl)->selEnd;
  326.     (*winTEHdl)->selStart = 0;
  327.     (*winTEHdl)->selEnd = 32767;
  328.  
  329.     stylHdl = GetStylHandle( winTEHdl );
  330.  
  331.     (*winTEHdl)->selStart = oldStart;
  332.     (*winTEHdl)->selEnd = oldEnd;
  333.     
  334.     SetStylHandle( stylHdl, gPrintTextHdl );
  335. #endif
  336.  
  337.     return;
  338. }
  339. #endif
  340.